package com.example.demo;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class PdfBox {

    public static void main(String[] args) throws IOException {
        imageToPdf("C:\\tempFiles\\test.png","C:\\tempFiles\\test.pdf");

    }


    /**
     *
     * @param source  要转换的word文件
     * @param target  要转换成为的pdf文件
     * @return
     */
//    public static boolean word2pdf(String source, String target) {
//        System.out.println("Word转PDF开始启动...");
//        long start = System.currentTimeMillis();
//        ActiveXComponent app = null;
//        try {
//            app = new ActiveXComponent("Word.Application");
//            app.setProperty("Visible", false);
//            Dispatch docs = app.getProperty("Documents").toDispatch();
//            System.out.println("打开文档：" + source);
//            Dispatch doc = Dispatch.call(docs, "Open", source, false, true).toDispatch();
//            System.out.println("转换文档到PDF：" + target);
//            File tofile = new File(target);
//            if (tofile.exists()) {
//                tofile.delete();
//            }
//            Dispatch.call(doc, "SaveAs", target, wdFormatPDF);
//            Dispatch.call(doc, "Close", false);
//            long end = System.currentTimeMillis();
//            System.out.println("转换完成，用时：" + (end - start) + "ms");
//            return true;
//        } catch (Exception e) {
//            System.out.println("Word转PDF出错：" + e.getMessage());
//            return false;
//        } finally {
//            if (app != null) {
//                app.invoke("Quit", wdDoNotSaveChanges);
//            }
//        }
//    }


    public static void imageToPdf(String sourcePath, String outPath) throws IOException {
        BufferedImage image = ImageIO.read(new File(sourcePath));

        PDDocument outDocument = new PDDocument();

        PDImageXObject imageXObject = LosslessFactory.createFromImage(outDocument, image);
        PDPage pdPage = new PDPage(PDRectangle.A4);
        outDocument.addPage(pdPage);
        PDPageContentStream pageContentStream = new PDPageContentStream(outDocument, pdPage);
        float height = pdPage.getMediaBox().getHeight();//要将图片在pdf中绘制多高，这里宽度直接使用了pdfpage的宽度，即横向铺满，这里的height也是使用了pdfpage的高度。因此最终结果是铺满整个pdf页。
        float y = pdPage.getMediaBox().getHeight() - height;//这里比较奇葩的是这个坐标是以左下角为原点坐标的。
        pageContentStream.drawImage(imageXObject, 0, y, pdPage.getMediaBox().getWidth(), height);
        pageContentStream.close();

        File outFile = new File(outPath);
        if (!outFile.getParentFile().exists()) {
            outFile.getParentFile().mkdirs();
        }

        outDocument.save(outFile);
        outDocument.close();
    }

//    public void printWithDialog(String filePath) {
//        try {
//            URL url = new URL(filePath);
//            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//            PDDocument document = PDDocument.load(connection.getInputStream());
//            //打印本地文件
//            // PDDocument document = PDDocument.load(new File(filaPath));
//            PrinterJob job = PrinterJob.getPrinterJob();
//            job.setPageable(new PDFPageable(document));
//            if (job.printDialog()){
//                job.print();
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (PrinterException e) {
//            e.printStackTrace();
//        }
//    }
}
